package com.leizi.igym.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leizi.igym.domain.Lost;
import com.leizi.igym.dto.LostAddDTO;
import com.leizi.igym.dto.LostDTO;
import com.leizi.igym.dto.LostESDTO;
import com.leizi.igym.dto.LostPageQueryDTO;
import com.leizi.igym.enums.ReturnCodeEnum;
import com.leizi.igym.exception.BusinessException;
import com.leizi.igym.manager.CosManager;
import com.leizi.igym.resp.PageResult;
import com.leizi.igym.resp.Result;
import com.leizi.igym.service.LostService;
import com.leizi.igym.utils.ThrowUtils;
import com.leizi.igym.vo.LostVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Collections;
import java.util.Objects;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-04-05  17:48
 * @Description:
 */

@RestController("userLostController")
@RequestMapping("/user/lost")
@Tag(name = "用户端-失物招领相关接口")
@Slf4j
public class LostController {

    @Resource
    private LostService lostService;

    @Resource
    private CosManager cosManager;

    @Operation(summary = "用户端：新增失物招领信息")
    @PostMapping
    public Result add(@RequestBody LostAddDTO dto) {
        log.info("用户端正在失物招领信息，失物招领信息为：{}", dto);
        ThrowUtils.throwIf(Objects.isNull(dto), ReturnCodeEnum.PARAMS_ERROR);
        boolean result = lostService.saveInAdminAndUser(dto);
        if (!result) {
            throw new BusinessException(ReturnCodeEnum.OPERATION_ERROR, "添加失物招领信息失败！");
        }
        return Result.ok(result);
    }

    @Operation(summary = "用户端：删除失物招领信息")
    @DeleteMapping("{id}")
    public Result deleteLost(@PathVariable("id") Long id) throws IOException {
        log.info("用户端正在删除失物招领信息，失物招领id为：{}", id);
        ThrowUtils.throwIf(Objects.isNull(id) || id <= 0, ReturnCodeEnum.PARAMS_ERROR);
        boolean result = lostService.deleteLostInAdminAndUser(id);
        if (!result) {
            throw new BusinessException(ReturnCodeEnum.SYSTEM_ERROR, "删除失物招领信息失败！");
        }
        return Result.ok(result);
    }


    @Operation(summary = "用户端：修改失物招领信息")
    @PutMapping("{id}")
    public Result modifyLost(@PathVariable("id") Long id, @RequestBody LostDTO dto) throws IOException {
        log.info("用户端正在修改失物招领信息，id为：{}，修改失物招领为：{}", id, dto);
        ThrowUtils.throwIf(Objects.isNull(dto) ||
                        id == null ||
                        id <= 0 ||
                        !id.equals(dto.getId()),
                ReturnCodeEnum.PARAMS_ERROR);
        Lost dbLost = lostService.getById(id);
        if (Objects.isNull(dbLost)) {
            throw new BusinessException(ReturnCodeEnum.UPDATE_ERROR, "该条失物招领信息异常！");
        }
        boolean result = lostService.updateInAdminAndUser(dto);
        if (!result) {
            throw new BusinessException(ReturnCodeEnum.SYSTEM_ERROR, "修改教练信息失败！");
        }
        return Result.ok(result);
    }

    @Operation(summary = "用户端：分页查询失物招领信息")
    @GetMapping("/{current}/{pageSize}")
    public Result<PageResult> getLostPage(@PathVariable("current") Long current,
                                             @PathVariable("pageSize") Long pageSize,
                                             LostPageQueryDTO dto) throws IOException {
        log.info("用户端正在分页查询失物招领信息，查询参数为：{}", dto);
        ThrowUtils.throwIf(Objects.isNull(dto), ReturnCodeEnum.PARAMS_ERROR);
        dto.setCurrent(current.intValue());
        dto.setPageSize(pageSize.intValue());
        long currentPage = dto.getCurrent();
        long size = dto.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 50 || currentPage <= 0 || size <= 0, ReturnCodeEnum.PARAMS_ERROR);
        Page<LostESDTO> page = lostService.getVOPage(dto);
        return page == null ? Result.ok(PageResult.builder()
                .records(Collections.emptyList())
                .total(0)
                .build()) : Result.ok(PageResult.builder()
                .records(page.getRecords())
                .total(page.getTotal())
                .build());
    }

    @Operation(summary = "用户端：根据id查询失物招领信息")
    @GetMapping("{id}")
    public Result<LostVO> getById(@PathVariable("id") Long id) {
        log.info("用户端正在根据id查询失物招领信息，id为：{}", id);
        ThrowUtils.throwIf(Objects.isNull(id) || id <= 0,
                ReturnCodeEnum.PARAMS_ERROR);

        LostVO lostVO = lostService.getVOById(id);
        if (Objects.isNull(lostVO)) {
            throw new BusinessException(ReturnCodeEnum.NOT_FOUND_ERROR, "该失物招领信息不存在！");
        }

        return Result.ok(lostVO);
    }
    
    
}
